/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomonovich.  All non-commercial  */
/* use is allowed, as long as this document not altered in any way, and    */
/* due credit is given.                                                    */
/***************************************************************************/

// AddInInt.cpp : implementation file

/*  Commands Exported By WndTabs Through AddInComm
    ==============================================

    Commands are case-insensitive.


    === IsSubclassed

        Syntax:  IsSubclassed hwnd_in_hex

        Params:  hwnd_in_hex is a window handle in hex.

        Returns:  1  subclassed by WndTabs
                  0  not subclassed by WndTabs
                 -1  error
                 
        Use this command to check if a certain window is currently subclassed
        by WndTabs.


    === Subclass

        Syntax:  Subclass hwnd_in_hex

        Params:  hwnd_in_hex is the handle of the window to subclass, in hex.

        Returns:  1  subclassing was successful
                  0  no subclassing occurred (no error)
                 -1  general error
                 -2  already subclassed
                 -3  subclassing failed
                 -4  illegal window
                 
        Use this command to re-subclass windows that were unsubclassed using 
        the "Unsubclass" command.  No other windows should be used.


    === Unsubclass

        Syntax:  Unsubclass hwnd_in_hex

        Params:  hwnd_in_hex is the handle of the window to unsubclass, in 
                    hex.

        Returns:  1  unsubclassing was successful
                  0  no unsubclassing occurred (no error)
                 -1  general error
                 -2  already unsubclassed
                 -3  unsubclassing failed
                 -4  illegal window
                 
        Use this command to unsubclass windows that were subclassed by 
        WndTabs.  You should then re-subclass the windows using the 
        "Subclass" command.  Leaving a window unsubclassed can cause 
        instability, so re-subclass before DevStudio regains control.
*/

#include "stdafx.h"
#include "TabBars.h"
#include "TabManagerWnd.h"
#include "DSWindow.h"
#include "DevStudioWnd.h"

/*
#define ERR_SUCCESS             1
#define ERR_PARVE               0
#define ERR_GENERR             -1
#define ERR_ALREADYCLASSED     -2
#define ERR_CLASSINGFAILED     -3
#define ERR_ILLEGALWIN         -4

static CSubclassWnd **ppSubclassed[] =
{
    (CSubclassWnd **)(&pGlobalActiveDSWindow), 
    (CSubclassWnd **)(&pGlobalActiveManager), 
    (CSubclassWnd **)(&g_pDevStudioWnd)
};


int OnIsSubclassed(LPCTSTR pParams)
{
    HWND hWnd;

    if (sscanf(pParams, "%x", &hWnd) != 1)
        return ERR_GENERR;

    if (hWnd)
    {
        for (int i = 0; i < countof(ppSubclassed); i++)
        {
            if (*ppSubclassed[i]  &&  hWnd == (**ppSubclassed[i]).m_hWnd)
                return 1;
        }
    }

    return 0;
}

int SubclassFns(LPCTSTR pParams, bool bDoSubclass)
{
    int             iRetVal = ERR_PARVE;
    HWND            hWnd;
    CSubclassWnd *  pWnd = NULL;

    if (sscanf(pParams, "%x", &hWnd) != 1)
        return ERR_GENERR;

    if (hWnd)
    {
        for (int i = 0; i < countof(ppSubclassed); i++)
        {
            if (*ppSubclassed[i]  &&  
                hWnd == (**ppSubclassed[i]).m_Saved_hWnd)
            {
                pWnd = *ppSubclassed[i];
                break;
            }
        }
        if (!pWnd)
        {
            iRetVal = ERR_ILLEGALWIN;
            TRACE("TabBars: Unknown window handle passed to On%subclass, "
                  "HWND=%p", (bDoSubclass? "S" : "Uns"), hWnd);
        }
    }

    if (pWnd)
    {
        if (pWnd->m_bIsSubclassed == bDoSubclass)
        {
            iRetVal = ERR_ALREADYCLASSED;
        }
        else
        {
            if (bDoSubclass)
                pWnd->DoSubclass();
            else
                pWnd->DoUnsubclass();

            iRetVal = (pWnd->m_bIsSubclassed == bDoSubclass)? 
                ERR_SUCCESS : ERR_CLASSINGFAILED;
        }
    }

    return iRetVal;
}

int OnSubclass(LPCTSTR pParams)
{
    return SubclassFns(pParams, true);
}

int OnUnsubclass(LPCTSTR pParams)
{
    return SubclassFns(pParams, false);
}

struct command_t
{
    LPCTSTR pName;
    int (*pFn)(LPCTSTR pParams);
};

static command_t commands[] = 
{
    { "IsSubclassed",   OnIsSubclassed },
    { "Subclass",       OnSubclass },
    { "Unsubclass",     OnUnsubclass },

    // short hand for for debugging  *** will be removed in release
    { "IS", OnIsSubclassed },
    { "S",  OnSubclass },
    { "U",  OnUnsubclass }
};


// callback for AddInComm
int AddInCallback(LPCTSTR pCmd)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    char buf[1024];
    int i, p;

    // isolate command string (command string -> buf)
    p = 0;
    while (pCmd[p]  &&   isspace(pCmd[p])) p++;
    while (pCmd[p]  &&  !isspace(pCmd[p])) p++;
    strncpy(buf, pCmd, p);
    buf[p] = '\0';

    // see if it's a known command
    for (i = 0; i < countof(commands); i++)
    {
        if (_tcsicmp(buf, commands[i].pName) == 0)
        {
            // trim white space from beginning of params
            while (pCmd[p]  &&  isspace(pCmd[p])) p++;
   
            return (commands[i].pFn(pCmd + p));
        }
    }

    return -1;
}

*/